<?php

namespace app\admin\controller\order;

use app\admin\model\Deviceorderdetail;
use app\api\service\DeviceService;
use app\api\service\VbillService;
use app\common\controller\Backend;

/**
 * 设备订单列管理
 *
 * @icon fa fa-circle-o
 */
class Deviceorder extends Backend
{

    /**
     * Deviceorder模型对象
     * @var \app\admin\model\Deviceorder
     */
    protected $model = null;
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Deviceorder;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->with(['user','agent'])
                ->where($where)
                ->where('Deviceorder.status',"<>",'paying')
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                if($v['channel'] == 'sxf'){
                    $list[$k]['channel'] = "随行付";
                }else if($v['channel'] == 'xs'){
                    $list[$k]['channel'] = "新生支付";
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function doRefund()
    {

        $id = $this->request->param('id');
        $orderModel = new \app\admin\model\Deviceorder();
        $order = $orderModel->where('id',$id)->find();
        $isOk = -1;
        if($order['status'] == 'payed'){
            $isOk = 1;
        }
        if($order['status'] == 'fail'){
            $isOk = 1;
        }
        if($isOk == -1){
            $this->error("此订单不符合退款状态");
        }


        $req = array();
        $req['mno']=$order['mno'];
        $req['ordNo']= getOrderSn();
        $req['origOrderNo']= $order['order_no'];
        $req['amt']=$order['price'];


        error_log("==refund req===".json_encode($req));
        $vbillService = new VbillService();
        $ret = $vbillService->refund($req);
        error_log("=====refund=sxf==".$ret);
        $result = json_decode($ret,true);
        if($result['respData']['bizCode'] != "0000"){
            $this->error($result['respData']['bizMsg']);
        }
        $orderGoodModel = new Deviceorderdetail();
        $orderModel->where('id',$order['id'])->update(['status'=>'refund']);
        $orderGoodModel->where('order_id',$order['id'])->update(['status'=>'refund']);

        $this->success('成功');
    }


    public function outAgain()
    {
        $id = $this->request->param('id');

        $user = $this->auth->getUserInfo();
        $user = $this->auth->getUser();
        $orderModel = new \app\admin\model\Deviceorder();
        $order = $orderModel->where('id',$id)->find();
        if($order['status'] != 'fail') $this->error("此订单不符合重新出货");


        $subOrderNo = $order['device_id'].getSubOrderNo();
        $orderModel->where('id',$order['id'])->update(['sub_order_no'=>$subOrderNo]);

        $deviceOrderDetailModel = new Deviceorderdetail();
        $deviceOrderDetailModel->where('order_id',$order['id'])->update(['order_no'=>$subOrderNo]);

        $orderDetail = $deviceOrderDetailModel->where('order_id',$order['id'])->find();
        $req = array();
        $req['order_no'] = $order['sub_order_no'];
        $req['slot'] =$orderDetail['slot'];
        $req['device_id'] =  $order['device_id'];
        $req['user_id'] =  $user['id'];

        $deviceService = new DeviceService();
        $deviceService->ship($req);

        $this->success('成功');
    }

}
